import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.IOException;

public class getHtmlContent {
    @Test
    public void text(){
        String url = "http://www.dytt8.net";

        Get_Url(url);
    }

    public void Get_Url(String url) {
        try {
            Document doc = Jsoup.connect(url)
                    //.data("query", "Java")
                    //.userAgent("头部")
                    //.cookie("auth", "token")
                    //.timeout(3000)
                    //.post()
                    .get();
            //System.out.println(doc);
            //得到html的所有东西
            Element content = doc.getElementById("header");
            //Elements movies = doc.getElementsByClass("inddline");
            //分离出html下<a>...</a>之间的所有东西
            //Elements links = content.getElementsByTag("a");
            Elements links = doc.select("td.inddline>a[href]");
            // 扩展名为.png的图片
            Elements pngs = doc.select("img[src$=.png]");
            // class等于masthead的div标签
            Element masthead = doc.select("div.masthead").first();

            for (Element link : links) {
                //得到<a>...</a>里面的网址
                String linkHref = link.attr("href");
                //得到<a>...</a>里面的汉字
                String linkText = link.text();
                System.out.println(linkHref);
                System.out.println(linkText);
                getImgAndUrl(url+""+linkHref);
               //Document document = Jsoup.connect(url+""+linkHref).get();
               // System.out.println(document);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getImgAndUrl(String url){
        try {
            Document doc = Jsoup.connect(url)
                    //.data("query", "Java")
                    //.userAgent("头部")
                    //.cookie("auth", "token")
                    //.timeout(3000)
                    //.post()
                    .get();
            Elements pngs = doc.select("div#Zoom img[src$=.jpg]");
            if(!pngs.isEmpty()) {
                String pngUrl = pngs.first().attr("src");
                System.out.println(pngUrl);
                Elements downloadUrl = doc.select("table a[href]");
                if(!downloadUrl.isEmpty()) {
                    String download = downloadUrl.first().attr("href");
                    System.out.println(download);
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}


